package com.adams.custom.page.mgt.dao.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.adams.access.db.bean.ViewEvent;
import com.adams.access.db.bean.ViewItem;
import com.adams.access.db.bean.ViewPanel;
import com.adams.access.db.dao.hibernate.HibernateGenericDao;
import com.adams.core.exception.BusinessException;
import com.adams.custom.page.util.ViewCacheUtil;

/**
 * 
 * File Name : ViewPanelDaoImpl.java
 * 
 * @Description : 页面面板实体数据访问实现类
 */
public class ViewPanelDaoImpl extends HibernateGenericDao<ViewPanel, Long>
{
	private static String cacheMapByPk_key = "ViewPanel_cacheMapByPk";

	private static String cacheMapByViewwinid_key = "ViewPanel_cacheMapByViewwinid";

	/**
	 * Description : 页面信息项实体数据访问实现类 Author : 904370
	 */
	private ViewItemDaoImpl viewItemDaoImpl;

	/**
	 * Description : 页面面板表格按钮访问实现类 Author : 904202
	 */
	private ViewEventDaoImpl viewEventDaoImpl;

	public void setViewEventDaoImpl(ViewEventDaoImpl viewEventDaoImpl)
	{
		this.viewEventDaoImpl = viewEventDaoImpl;
	}

	/**
	 * @param viewItemDaoImpl
	 *            the viewItemDaoImpl to set
	 */
	public void setViewItemDaoImpl(ViewItemDaoImpl viewItemDaoImpl)
	{
		this.viewItemDaoImpl = viewItemDaoImpl;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.adams.access.db.dao.hibernate.HibernateGenericDao#save(java
	 * .lang.Object)
	 */
	public void save(ViewPanel model)
	{
		super.save(model);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.adams.access.db.dao.GenericDao#get(java.io.Serializable)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public ViewPanel get(Serializable id)
	{
		String key = id + "";
		Map cacheMapByPk = ViewCacheUtil
				.getConfigDataCacheMap(cacheMapByPk_key);
		if (!cacheMapByPk.containsKey(key))
		{
			synchronizationCache();
		}
		return (ViewPanel) cacheMapByPk.get(key);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.adams.access.db.dao.hibernate.HibernateGenericDao#delete(java
	 * .lang.Object)
	 */
	public void delete(ViewPanel model)
	{
		super.delete(model);
	}

	/**
	 * Description : 删除ViewPanel列表
	 * 
	 * @param models
	 * 
	 */
	public void deleteAll(List<ViewPanel> models)
	{
		for (ViewPanel model : models)
		{
			super.delete(model);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.adams.access.db.dao.hibernate.HibernateGenericDao#saveOrUpdate
	 * (java.lang.Object)
	 */
	public void saveOrUpdate(ViewPanel model)
	{
		super.saveOrUpdate(model);
	}

	/**
	 * Description : 根据页面窗口id得到ViewPanel列表
	 * 
	 * @param viewwinid
	 * @return
	 * 
	 */
	@SuppressWarnings("unchecked")
	public List<ViewPanel> findByViewwinid(Long viewwinid)
	{
		String key = viewwinid + "";
		Map cacheMapByViewwinid = ViewCacheUtil
				.getConfigDataCacheMap(cacheMapByViewwinid_key);
		if (!cacheMapByViewwinid.containsKey(key))
		{
			synchronizationCache();
		}
		List<ViewPanel> result = new ArrayList<ViewPanel>();
		List<ViewPanel> temp = (List<ViewPanel>) cacheMapByViewwinid.get(key);
		if (null != temp)
		{
			result.addAll(temp);
			// sort(result);
		}
		return result;
	}

	private void sort(List<ViewPanel> result)
	{
		if (null == result || result.size() == 0)
		{
			return;
		}
		Collections.sort(result, new Comparator<ViewPanel>()
		{
			public int compare(ViewPanel o1, ViewPanel o2)
			{
				return o1.getId().compareTo(o2.getId());
			}
		});
	}

	/**
	 * Description : 得到所有ViewPanel对象
	 * 
	 * @return
	 * 
	 */
	@SuppressWarnings("unchecked")
	public List<ViewPanel> findAll()
	{
		List<ViewPanel> result = null;
		Map cacheMapByPk = ViewCacheUtil
				.getConfigDataCacheMap(cacheMapByPk_key);
		if (cacheMapByPk.size() == 0)
		{
			result = synchronizationCache();
			return result;
		} else
		{
			result = new ArrayList<ViewPanel>();
			Set keySet = cacheMapByPk.keySet();
			for (Iterator it = keySet.iterator(); it.hasNext();)
			{
				Object key = it.next();
				ViewPanel temp = (ViewPanel) cacheMapByPk.get(key);
				if (null != temp)
				{
					result.add(temp);
				}
			}
		}
		return result;
	}

	private List<ViewPanel> findAllFromDB()
	{
		List<ViewPanel> result = query("from ViewPanel");
		return result;
	}

	/**
	 * Description : 同步缓存
	 * 
	 * @return
	 * 
	 */
	@SuppressWarnings("unchecked")
	public synchronized List<ViewPanel> synchronizationCache()
	{
		List<ViewPanel> result = findAllFromDB();
		Map cacheMapByPk = ViewCacheUtil
				.getConfigDataCacheMap(cacheMapByPk_key);
		Map cacheMapByViewwinid = ViewCacheUtil
				.getConfigDataCacheMap(cacheMapByViewwinid_key);
		cacheMapByViewwinid.clear();
		for (ViewPanel viewPanel : result)
		{
			cacheMapByPk.put(viewPanel.getId() + "", viewPanel);

			String viewwinid = viewPanel.getViewwinid() + "";
			List<ViewPanel> list = null;
			if (cacheMapByViewwinid.containsKey(viewwinid))
			{
				list = (List<ViewPanel>) cacheMapByViewwinid.get(viewwinid);
			} else
			{
				list = new ArrayList<ViewPanel>();
			}
			list.add(viewPanel);
			cacheMapByViewwinid.put(viewwinid, list);
		}

		// 排序处理
		Set<String> viewwinids = cacheMapByViewwinid.keySet();
		for (String viewwinid : viewwinids)
		{
			List<ViewPanel> viewPanels = (List<ViewPanel>) cacheMapByViewwinid
					.get(viewwinid);
			sort(viewPanels);
			cacheMapByViewwinid.put(viewwinid, viewPanels);
		}

		ViewCacheUtil.put2ConfigDataCacheMap(cacheMapByPk_key, cacheMapByPk);
		ViewCacheUtil.put2ConfigDataCacheMap(cacheMapByViewwinid_key,
				cacheMapByViewwinid);
		return result;
	}

	/**
	 * Description : 根据面板id得到ViewItem列表
	 * 
	 * @param viewpanelid
	 * @return
	 * @throws BusinessException
	 * 
	 */
	public List<ViewItem> findItems(Long viewpanelid) throws BusinessException
	{
		return viewItemDaoImpl.findItemByViewPanelid(viewpanelid);
	}

	/**
	 * Description : 根据面板id得到ViewEvent列表
	 * 
	 * @param viewpanelid
	 * @return
	 * 
	 */
	public List<ViewEvent> findEvents(Long viewpanelid)
	{
		return viewEventDaoImpl.findByViewpanelid(viewpanelid);
	}
}
